package com.mind.customer.api.helper;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mind.common.enums.DataType;
import com.mind.common.utils.StringUtils;
import com.mind.customer.mapper.AjObjectMapper;
import com.mind.system.domain.AjObject;
import com.mind.system.domain.vo.Field;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@Log4j2
public class FieldHelper {

    @Resource
    private AjObjectMapper objectMapper;

//    public String getFields(String type) {
//        List<AjObject> objectList = objectMapper.selectObjectList(new AjObject(){{setAttachCate(type);}});
//        if(CollectionUtils.isEmpty(objectList)){
//            log.error("该类型{}没有对象", type);
//            return "";
//        }
////        JSONArray result = new JSONArray();
//        JSONObject jsonObject = new JSONObject();
//        Set<String> objectSet  = new HashSet<>();
//        Set<String> arraySet = new HashSet<>();
//        for(AjObject object: objectList){
//            List<Field> fields = JSONArray.parseArray(object.getFields(), Field.class);
//            for(Field field: fields){
//                objectSet.add(field.getRuleDataField());
//                if(DataType.HEADER.getCode().equals(field.getDataType())){
//                   if(StringUtils.isNotEmpty(field.getAttachmentDataField())){
//                       objectSet.addAll(Arrays.asList(field.getAttachmentDataField().split(",")));
//                   }
//                }
//                if(DataType.ROW.getCode().equals(field.getDataType())){
//                    for(Field f:field.getChildren()){
//                        arraySet.addAll(Arrays.asList(field.getAttachmentDataField().split(",")));
//                        arraySet.add(f.getRuleDataField());
//                    }
//                }
//            }
//        }
//        jsonObject.put("data_search_words", String.join(",", objectSet));
//        jsonObject.put("data_search_headers", String.join(",", arraySet));
//        return JSONObject.toJSONString(jsonObject);
//    }

    public String getFields(String type) {
        List<AjObject> objectList = objectMapper.selectObjectList(new AjObject(){{setAttachCate(type);}});
        if(CollectionUtils.isEmpty(objectList)){
            log.error("该类型{}没有对象", type);
            return "";
        }
//        JSONArray result = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        Set<String> objectSet  = new HashSet<>();
        Set<String> arraySet = new HashSet<>();
        for(AjObject object: objectList){
            List<Field> fields = JSONArray.parseArray(object.getFields(), Field.class);
            for(Field field: fields){
                if(DataType.HEADER.getCode().equals(field.getDataType())){
                    String filed = field.getAttachmentDataField();
                    if(!filed.startsWith("一般表示为")){
                        filed = "一般表示为" + filed;
                    }
                    if(StringUtils.isNotEmpty(field.getAttachmentDataField())){
                        jsonObject.put(field.getRuleDataField(), filed);
                    }
                }
                if(DataType.ROW.getCode().equals(field.getDataType()) && !field.getAttachmentDataField().equals("SIGNATURE_STAMP")){
                    JSONArray array = new JSONArray();
                    JSONObject json = new JSONObject();
                    for(Field f: field.getChildren()){
                        String filed = f.getAttachmentDataField();
                        if(!filed.startsWith("一般表示为")){
                            filed = "一般表示为" + filed;
                        }
                        json.put(f.getRuleDataField(), filed);

                    }
                    array.add(json);
                    jsonObject.put(field.getRuleDataField(), array);
                }
            }
        }
        return JSONObject.toJSONString(jsonObject);
    }
}
